<?php
/**
 * WordPress AJAX Process Execution.
 *
 * @package WordPress
 * @subpackage Administration
 */

/**
 * Executing AJAX process.
 *
 * @since unknown
 */
  
define('DOING_AJAX', true);
define('WP_ADMIN', true);

require_once('../../../wp-load.php');

if( ! isset($_POST['action']))
	die('-1');

require_once('../../../wp-admin/includes/admin.php');
@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
send_nosniff_header();

do_action('admin_init');

if ( ! is_user_logged_in() ) {
	$result['STATUS'] = 'error';
	$result['ERROR_MESSAGE'] = 'not_logged';
	echo json_encode($result);
	die();
}
else{
	switch ( $action = $_POST['action'] ) {
		case'change_language':
			update_option('gahb_language',$_POST['gahb_language']);
			$result['STATUS'] = 'ok';
			echo json_encode($result);
			die();
		break;
		case'save_gahb_print_settings':
			$values_s = $_POST['values'];
			$temp = explode('&',$values_s);
			foreach($temp as $v){
				$t = explode('=',$v);
				$values[$t[0]] = $t[1];	
			}
			$save = json_encode($values);
			update_option('gahb_print_settings',$save);
			$result['STATUS'] = 'error';
			if($save==get_option(gahb_print_settings))
				$result['STATUS'] = 'ok';
			echo json_encode($result);
			die();
		break;
		case'save_gahb_oh_general_settings':
			$gahb_language = $_POST['gahb_language'];
			update_option('gahb_language',$gahb_language);

			$gahb_oh_paypal_account = $_POST['gahb_oh_paypal_account'];			
			$update_paypal_account = json_decode(get_data('http://onlinehotel.com.br/api/aff_api.php',array('function_type'=>'UPDATE_PAYPAL_ACCOUNT','gahb_oh_paypal_account'=>$gahb_oh_paypal_account, 'API_KEY'=>get_option(gahb_oh_api_key))));
			if($update_paypal_account->STATUS == 'ok'){
				$result['STATUS'] = 'ok';
				update_option('gahb_oh_paypal_account', $gahb_oh_paypal_account);
			}
			else{
				$result['STATUS'] = 'error';
				$result['ERROR_MESSAGE'] = 'cant_update_paypal';
			
			}
			echo json_encode($result);
			die();
		break;
		case'save_gahb_ads_general_settings':
			$gahb_ads_id = $_POST['gahb_ads_id'];
			$gahb_ads_chanel = $_POST['gahb_ads_chanel'];
			
			update_option('gahb_ads_id', $gahb_ads_id);
			update_option('gahb_ads_chanel', $gahb_ads_chanel);
			
			$result['STATUS'] = 'ok';
			echo json_encode($result);
			die();
		break;
		case'save_gahb_oh_new_cp':
			
			//INPUTS
			$data['function_type'] = 'SAVE_CP';
			$data['aff_code'] = get_option(gahb_oh_api_key);
			$data['parameter'] = $_POST['parameter'];
			$data['where'] = $_POST['where'];
			$data['name'] = $_POST['name'];
			$data['midia'] = $_POST['midia'];
			$data['where_name'] = $_POST['where_name'];

		
			$update_api_key = json_decode(get_data('http://onlinehotel.com.br/api/aff_api.php',$data));
			if($update_api_key->STATUS == 'ok'){
				$result['STATUS'] = 'ok';
			}
			echo json_encode($result);
			die();
		break;
		case'save_gahb_oh_edit_cp':
			
			//INPUTS
			$data['function_type'] = 'SAVE_CP';
			$data['aff_code'] = get_option(gahb_oh_api_key);
			$data['parameter'] = $_POST['parameter'];
			$data['where'] = $_POST['where'];
			$data['name'] = $_POST['name'];
			$data['midia'] = $_POST['midia'];
			$data['where_name'] = $_POST['where_name'];

		
			$update_api_key = json_decode(get_data('http://onlinehotel.com.br/api/aff_api.php',$data));
			if($update_api_key->STATUS == 'ok'){
				$result['STATUS'] = 'ok';
			}
			echo json_encode($result);
			die();
		break;
	}
}
function get_data($url, $_data) {
	ini_set("allow_url_fopen", 1);
	$data = array();    
	while(list($n,$v) = each($_data)){$data[] = "$n=$v";}    
	$data = implode('&', $data);
	$url = parse_url($url);
	$host = $url['host'];
	$path = $url['path'];
	$port = 80;
	$data_length = strlen($data);
	$header  = "POST $path HTTP/1.0\r\n";
	$header .= "Host: $host\r\n";
	$header .= "User-Agent: DoCoMo/1.0/P503i\r\n";
	$header .= "Content-type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-length: $data_length\r\n";
	$header .= "\r\n";
	$fp = fsockopen($host,$port,$err_num,$err_msg,120);
	fputs($fp, $header . $data);
	while(trim(fgets($fp,4096)) != '');//Retira o cabecalho HTTP
	while(!feof($fp)){$response .= fgets($fp,4096);}
	// close the socket connection:
	fclose($fp);
	return $response;
}

?>
